#include "loginwindow.h"
#include "../../ui/ui_loginwindow.h"
#include "sm3_utils.h"
#include "../../ui/ui_mainwindow.h"
#include "mainwindow.h"
#include "adminwindow.h"
#include "../../ui/ui_adminwindow.h"
#include "database.h"

#include <QMessageBox>
#include <QDebug>

LoginWindow::LoginWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LoginWindow),
    client(this),
    isLoginInProgress(false)
{
    ui->setupUi(this);
    //connect(ui->loginButton, &QPushButton::clicked, this, &LoginWindow::on_loginButton_clicked);
    //connect(ui->registerButton, &QPushButton::clicked, this, &LoginWindow::on_registerButton_clicked);
}

LoginWindow::~LoginWindow() {
    delete ui;
}

void LoginWindow::on_loginButton_clicked() {
    if (isLoginInProgress) return;
    isLoginInProgress = true;
    ui->loginButton->setEnabled(false);

    QString username = ui->usernameEdit->text().trimmed();
    QString password = ui->passwordEdit->text();

    if (username.isEmpty() || password.isEmpty()) {
        QMessageBox::warning(this, "输入错误", "用户名或密码不能为空");
        ui->loginButton->setEnabled(true);
        isLoginInProgress = false;
        return;
    }

    QString hashedPassword = sm3_hash(password);

    if (!client.connectToServer("127.0.0.1", 8888)) {
        QMessageBox::critical(this, "连接失败", "无法连接服务器");
        ui->loginButton->setEnabled(true);
        isLoginInProgress = false;
        return;
    }

    if (!client.sendCommand(QString("LOGIN %1 %2").arg(username, hashedPassword))) {
        QMessageBox::warning(this, "发送失败", "登录指令发送失败");
        client.closeConnection();
        ui->loginButton->setEnabled(true);
        isLoginInProgress = false;
        return;
    }

    QString reply = client.readResponse();
    client.closeConnection();

    if (reply == "LOGIN_SUCCESS") {
        QMessageBox::information(this, "登录成功", QString("欢迎回来，%1").arg(username));
        if (!Database::openConnectionForThread()) {
            QMessageBox::warning(this, "警告", "无法连接数据库，权限判断失败。");
        }
        if (Database::isAdmin(username.toStdString())) {
            AdminWindow *adminWin = new AdminWindow(username);
            adminWin->show();
        } else {
            MainWindow *mainWin = new MainWindow(username);
            mainWin->show();
        }   
        this->close(); // 关闭登录窗口
        return;
    } else {
        QMessageBox::warning(this, "登录失败", "用户名或密码错误");
    }

    ui->loginButton->setEnabled(true);
    isLoginInProgress = false;
}

void LoginWindow::on_registerButton_clicked() {
    ui->registerButton->setEnabled(false);

    QString username = ui->usernameEdit->text().trimmed();
    QString password = ui->passwordEdit->text();

    if (username.isEmpty() || password.isEmpty()) {
        QMessageBox::warning(this, "输入错误", "用户名或密码不能为空");
        ui->registerButton->setEnabled(true);
        return;
    }

    QString hashedPassword = sm3_hash(password);

    if (!client.connectToServer("127.0.0.1", 8888)) {
        QMessageBox::critical(this, "连接失败", "无法连接服务器");
        ui->registerButton->setEnabled(true);
        return;
    }

    client.sendCommand(QString("REGISTER %1 %2").arg(username, hashedPassword));
    QString reply = client.readResponse();
    client.closeConnection();

    if (reply == "REGISTER_SUCCESS") {
        QMessageBox::information(this, "注册成功", "用户已成功创建");
    } else {
        QMessageBox::warning(this, "注册失败", "用户已存在或服务器错误");
    }

    ui->registerButton->setEnabled(true);
}
